import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpRequest } from '../../../../service/httpRequest';
import { flyIn } from '../../../../animations/fly-in';
import { ModalDirective } from 'ng2-bootstrap/modal';
declare let $:any;
@Component({
  selector: 'app-device-point',
  templateUrl: './device-point.component.html',
  styleUrls: ['./device-point.component.css'],
  animations: [
    flyIn
  ]
})
export class DevicePointComponent implements OnInit {
  deviceId:string = '';
  pointData:any[];
  pointmsg:string = '';
  constructor(private activeRoute:ActivatedRoute,private httpRequest:HttpRequest) { }

  ngOnInit() {
    this.activeRoute.params.subscribe(params=>{
      if(params['deviceId'] == 'none') {
        this.deviceId = '';
      }
      else{
        this.deviceId = params['deviceId'];
      }     
      
    }); 
    this.getPointdata()  
  }


  getPointdata(){
    this.httpRequest.sendPost('device_property_of_realtime/page',{
      device_id:this.deviceId.replace('|','/'),
      pid:this.deviceId.split('|')[0]
    }).subscribe(res=>{
      //console.log(res)
      if(res.code==700){
        this.pointmsg = res.msg;
      }else{

        this.pointData = res;
      }
     
    });
  }
  checkAll(ev){
      this.pointData.forEach(x => x.group_show = ev.target.checked)
  }

  isAllChecked() {   
    if(this.pointData){
      return this.pointData.every(_ => _.group_show);
    }    
  }

  comfirmdata(){
      let didstr=Array();
      for(var i=0;i<this.pointData.length;i++){
          if(this.pointData[i].group_show){
              didstr.push(this.pointData[i].property)
          }          
      }

      if(didstr.length==0){
        this.httpRequest.sendPost('device_property_of_realtime/deleteAll',{
          device_id:this.deviceId.replace('|','/')         
        }).subscribe(res=>{
           //console.log(res)
           if(res.resultStatus==200){
            alert('没有选择任何参数点显示！')
            this.getPointdata();
           }                
         
        });
      }else  if(didstr.length==this.pointData.length){
        this.httpRequest.sendPost('device_property_of_realtime/addAll',{
          device_id:this.deviceId.replace('|','/'),
          pid:this.deviceId.split('|')[0]         
        }).subscribe(res=>{
           //console.log(res)
           if(res.resultStatus==200){
            alert('选择了全部参数显示！')
            this.getPointdata();
           }        
         
        });
      }else{
        //console.log(didstr)
        this.httpRequest.sendPost('device_property_of_realtime/update',{
          device_id:this.deviceId.replace('|','/'),
          pid:this.deviceId.split('|')[0],
          properties:didstr.join(',')
        }).subscribe(res=>{
           //console.log(res)
           if(res.resultStatus==200){
            alert("选择参数显示成功！")
            this.getPointdata();
           }   
        });
      }



  } 

                

 
}
